from django.db import transaction
from django.db.models import F

from user.models import *


def match(data, order):
    if order['lock'] == True:
        return
    

    if order['order_type'] == 'buy':
        filled = 0

        for i in range(len(data['sell'])):
            ask = data['sell'][i]
            if ask['lock'] == True:
                break

            data[i]['lock'] = True

            if ask.price > order.price:
                break  # 卖价过高

            if order == 0:
                break  # 已经买够

            if ask['price'] != order['price'] or order['lock'] == True:
                continue
            else:
                order['lock'] = True

            if ask['number_shares'] == order['number_shares']:  # 数量正合适
                # 创建交易订单
                Deal.objects.create(buy_uid=order['uid'], sell_uid=ask['uid'], price=order['price'],
                                    number_shares=order['number_shares'])
                # 从列表中删除此订单
                data['sell'].pop(i)
                data['sum_list'].pop(data['sum_list'].index(ask))
                # 从总列表中删除当前匹配订单
                data['sum_list'].pop(data['sum_list'].index(order))
                # 删除库中订单
                TransactionOrder.objects.get(id=order['id']).delete()
                TransactionOrder.objects.get(id=ask['id']).delete()

            elif ask['number_shares'] > order['number_shares']:
                # 创建交易订单
                Deal.objects.create(buy_uid=order['uid'], sell_uid=ask['uid'], price=order['price'],
                                    number_shares=order['number_shares'])
                # 修改卖家股数
                data['sell'][i]['number_shares'] = order['number_shares']
                data['sum_list'][data['sum_list']]['number_shares'] = order['number_shares']
                # 从总列表中删除当前匹配订单
                data['sum_list'].pop(data['sum_list'].index(order))
                # 删除库中订单
                TransactionOrder.objects.get(id=order['id']).delete()
                # 修改裤中卖家股数
                TransactionOrder.objects.filter(id=ask['id']).update(
                    number_shares=F('number_shares') - order['number_shares'])

            elif ask['number_shares'] < order['number_shares']:
                # 创建交易订单
                Deal.objects.create(buy_uid=order['uid'], sell_uid=ask['uid'], price=order['price'],
                                    number_shares=order['number_shares'])

                # 修改当前订单股数
                order['number_shares'] = order['number_shares'] - ask['number_shares']
                # 删除卖家订单
                data['sell'].pop(i)
                data['sum_list'].pop(data['sum_list'].index(ask))

                # 修改库中订单
                TransactionOrder.objects.filter(id=order['id']).update(
                    number_shares=F('number_shares') - ask['number_shares'])
                # 删除库中卖家股数
                TransactionOrder.objects.get(id=ask['id']).delete()

    elif order['order_type'] == 'sell':
        filled = 0

        for i in range(len(data['buy'])):
            ask = data['sell'][i]
            if ask.price < order.price:
                break  # 买价过低

            if order == 0:
                break  # 已经卖完

            if ask['price'] != order['price'] or order['lock'] == True:
                continue
            else:
                order['lock'] = True


            if ask['number_shares'] == order['number_shares']:  # 数量正合适
                # 创建交易订单
                Deal.objects.create(buy_uid=order['uid'], sell_uid=ask['uid'], price=order['price'],
                                    number_shares=order['number_shares'])
                # 从列表中删除此订单
                data['buy'].pop(i)
                data['sum_list'].pop(data['sum_list'].index(ask))
                # 从总列表中删除当前匹配订单
                data['sum_list'].pop(data['sum_list'].index(order))

                # 删除库中订单
                TransactionOrder.objects.get(id=order['id']).delete()
                TransactionOrder.objects.get(id=ask['id']).delete()

            elif ask['number_shares'] > order['number_shares']:
                # 创建交易订单
                Deal.objects.create(buy_uid=order['uid'], sell_uid=ask['uid'], price=order['price'],
                                    number_shares=order['number_shares'])
                # 修改卖家股数
                data['buy'][i]['number_shares'] = order['number_shares']
                data['sum_list'][data['sum_list'].index(ask)]['number_shares'] = order['number_shares']
                # 从总列表中删除当前匹配订单
                data['sum_list'].pop(data['sum_list'].index(order))
                # 删除库中订单
                TransactionOrder.objects.get(id=order['id']).delete()
                # 修改库中卖家股数
                TransactionOrder.objects.filter(id=ask['id']).update(
                    number_shares=F('number_shares') - order['number_shares'])

            elif ask['number_shares'] < order['number_shares']:
                # 创建交易订单
                Deal.objects.create(buy_uid=order['uid'], sell_uid=ask['uid'], price=order['price'],
                                    number_shares=order['number_shares'])

                # 修改当前订单股数
                order['number_shares'] = order['number_shares'] - ask['number_shares']
                # 删除卖家订单
                data['buy'].pop(i)
                data['sum_list'].pop(data['sum_list'].index(data['sum_list']))

                # 修改库中订单
                TransactionOrder.objects.filter(id=order['id']).update(
                    number_shares=F('number_shares') - ask['number_shares'])
                # 删除库中卖家股数
                TransactionOrder.objects.get(id=ask['id']).delete()


def run(data):
    for i in data['sum_list']:
        match(data, i)
